package com.whiteape.book.model.gen;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.whiteape.book.model.dto.SiteSourceBean;
import com.whiteape.book.utils.MapConverter;
import java.util.HashMap;

import com.whiteape.book.model.dto.CollectionMediaBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "COLLECTION_MEDIA_BEAN".
*/
public class CollectionMediaBeanDao extends AbstractDao<CollectionMediaBean, String> {

    public static final String TABLENAME = "COLLECTION_MEDIA_BEAN";

    /**
     * Properties of entity CollectionMediaBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, String.class, "id", true, "ID");
        public final static Property MediaType = new Property(1, Integer.class, "mediaType", false, "MEDIA_TYPE");
        public final static Property Title = new Property(2, String.class, "title", false, "TITLE");
        public final static Property Author = new Property(3, String.class, "author", false, "AUTHOR");
        public final static Property CoverUrl = new Property(4, String.class, "coverUrl", false, "COVER_URL");
        public final static Property Desc = new Property(5, String.class, "desc", false, "DESC");
        public final static Property WordCount = new Property(6, String.class, "wordCount", false, "WORD_COUNT");
        public final static Property State = new Property(7, String.class, "state", false, "STATE");
        public final static Property Category = new Property(8, String.class, "category", false, "CATEGORY");
        public final static Property LastChapter = new Property(9, String.class, "lastChapter", false, "LAST_CHAPTER");
        public final static Property LastUpdated = new Property(10, String.class, "lastUpdated", false, "LAST_UPDATED");
        public final static Property LastRead = new Property(11, String.class, "lastRead", false, "LAST_READ");
        public final static Property ChaptersCount = new Property(12, Integer.class, "chaptersCount", false, "CHAPTERS_COUNT");
        public final static Property CurrentChapter = new Property(13, Integer.class, "currentChapter", false, "CURRENT_CHAPTER");
        public final static Property CurrentPage = new Property(14, Integer.class, "currentPage", false, "CURRENT_PAGE");
        public final static Property IfUpdated = new Property(15, Boolean.class, "ifUpdated", false, "IF_UPDATED");
        public final static Property IfLocal = new Property(16, Boolean.class, "ifLocal", false, "IF_LOCAL");
        public final static Property BookSourceUrl = new Property(17, String.class, "bookSourceUrl", false, "BOOK_SOURCE_URL");
        public final static Property Group = new Property(18, String.class, "group", false, "GROUP");
        public final static Property DiyTitle = new Property(19, String.class, "diyTitle", false, "DIY_TITLE");
        public final static Property DiyAuthor = new Property(20, String.class, "diyAuthor", false, "DIY_AUTHOR");
        public final static Property DiyDesc = new Property(21, String.class, "diyDesc", false, "DIY_DESC");
        public final static Property DiyCoverUrl = new Property(22, String.class, "diyCoverUrl", false, "DIY_COVER_URL");
        public final static Property Encrypted = new Property(23, Boolean.class, "encrypted", false, "ENCRYPTED");
        public final static Property VarMap = new Property(24, String.class, "varMap", false, "VAR_MAP");
        public final static Property CatalogUrl = new Property(25, String.class, "catalogUrl", false, "CATALOG_URL");
        public final static Property CatalogNextPageUrl = new Property(26, String.class, "catalogNextPageUrl", false, "CATALOG_NEXT_PAGE_URL");
    }

    private DaoSession daoSession;

    private final MapConverter varMapConverter = new MapConverter();

    public CollectionMediaBeanDao(DaoConfig config) {
        super(config);
    }
    
    public CollectionMediaBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"COLLECTION_MEDIA_BEAN\" (" + //
                "\"ID\" TEXT PRIMARY KEY NOT NULL ," + // 0: id
                "\"MEDIA_TYPE\" INTEGER NOT NULL ," + // 1: mediaType
                "\"TITLE\" TEXT NOT NULL ," + // 2: title
                "\"AUTHOR\" TEXT," + // 3: author
                "\"COVER_URL\" TEXT," + // 4: coverUrl
                "\"DESC\" TEXT," + // 5: desc
                "\"WORD_COUNT\" TEXT," + // 6: wordCount
                "\"STATE\" TEXT," + // 7: state
                "\"CATEGORY\" TEXT," + // 8: category
                "\"LAST_CHAPTER\" TEXT," + // 9: lastChapter
                "\"LAST_UPDATED\" TEXT," + // 10: lastUpdated
                "\"LAST_READ\" TEXT," + // 11: lastRead
                "\"CHAPTERS_COUNT\" INTEGER," + // 12: chaptersCount
                "\"CURRENT_CHAPTER\" INTEGER," + // 13: currentChapter
                "\"CURRENT_PAGE\" INTEGER," + // 14: currentPage
                "\"IF_UPDATED\" INTEGER," + // 15: ifUpdated
                "\"IF_LOCAL\" INTEGER," + // 16: ifLocal
                "\"BOOK_SOURCE_URL\" TEXT," + // 17: bookSourceUrl
                "\"GROUP\" TEXT," + // 18: group
                "\"DIY_TITLE\" TEXT," + // 19: diyTitle
                "\"DIY_AUTHOR\" TEXT," + // 20: diyAuthor
                "\"DIY_DESC\" TEXT," + // 21: diyDesc
                "\"DIY_COVER_URL\" TEXT," + // 22: diyCoverUrl
                "\"ENCRYPTED\" INTEGER," + // 23: encrypted
                "\"VAR_MAP\" TEXT," + // 24: varMap
                "\"CATALOG_URL\" TEXT," + // 25: catalogUrl
                "\"CATALOG_NEXT_PAGE_URL\" TEXT);"); // 26: catalogNextPageUrl
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"COLLECTION_MEDIA_BEAN\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, CollectionMediaBean entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
        stmt.bindLong(2, entity.getMediaType());
        stmt.bindString(3, entity.getTitle());
 
        String author = entity.getAuthor();
        if (author != null) {
            stmt.bindString(4, author);
        }
 
        String coverUrl = entity.getCoverUrl();
        if (coverUrl != null) {
            stmt.bindString(5, coverUrl);
        }
 
        String desc = entity.getDesc();
        if (desc != null) {
            stmt.bindString(6, desc);
        }
 
        String wordCount = entity.getWordCount();
        if (wordCount != null) {
            stmt.bindString(7, wordCount);
        }
 
        String state = entity.getState();
        if (state != null) {
            stmt.bindString(8, state);
        }
 
        String category = entity.getCategory();
        if (category != null) {
            stmt.bindString(9, category);
        }
 
        String lastChapter = entity.getLastChapter();
        if (lastChapter != null) {
            stmt.bindString(10, lastChapter);
        }
 
        String lastUpdated = entity.getLastUpdated();
        if (lastUpdated != null) {
            stmt.bindString(11, lastUpdated);
        }
 
        String lastRead = entity.getLastRead();
        if (lastRead != null) {
            stmt.bindString(12, lastRead);
        }
 
        Integer chaptersCount = entity.getChaptersCount();
        if (chaptersCount != null) {
            stmt.bindLong(13, chaptersCount);
        }
 
        Integer currentChapter = entity.getCurrentChapter();
        if (currentChapter != null) {
            stmt.bindLong(14, currentChapter);
        }
 
        Integer currentPage = entity.getCurrentPage();
        if (currentPage != null) {
            stmt.bindLong(15, currentPage);
        }
 
        Boolean ifUpdated = entity.getIfUpdated();
        if (ifUpdated != null) {
            stmt.bindLong(16, ifUpdated ? 1L: 0L);
        }
 
        Boolean ifLocal = entity.getIfLocal();
        if (ifLocal != null) {
            stmt.bindLong(17, ifLocal ? 1L: 0L);
        }
 
        String bookSourceUrl = entity.getBookSourceUrl();
        if (bookSourceUrl != null) {
            stmt.bindString(18, bookSourceUrl);
        }
 
        String group = entity.getGroup();
        if (group != null) {
            stmt.bindString(19, group);
        }
 
        String diyTitle = entity.getDiyTitle();
        if (diyTitle != null) {
            stmt.bindString(20, diyTitle);
        }
 
        String diyAuthor = entity.getDiyAuthor();
        if (diyAuthor != null) {
            stmt.bindString(21, diyAuthor);
        }
 
        String diyDesc = entity.getDiyDesc();
        if (diyDesc != null) {
            stmt.bindString(22, diyDesc);
        }
 
        String diyCoverUrl = entity.getDiyCoverUrl();
        if (diyCoverUrl != null) {
            stmt.bindString(23, diyCoverUrl);
        }
 
        Boolean encrypted = entity.getEncrypted();
        if (encrypted != null) {
            stmt.bindLong(24, encrypted ? 1L: 0L);
        }
 
        HashMap varMap = entity.getVarMap();
        if (varMap != null) {
            stmt.bindString(25, varMapConverter.convertToDatabaseValue(varMap));
        }
 
        String catalogUrl = entity.getCatalogUrl();
        if (catalogUrl != null) {
            stmt.bindString(26, catalogUrl);
        }
 
        String catalogNextPageUrl = entity.getCatalogNextPageUrl();
        if (catalogNextPageUrl != null) {
            stmt.bindString(27, catalogNextPageUrl);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, CollectionMediaBean entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
        stmt.bindLong(2, entity.getMediaType());
        stmt.bindString(3, entity.getTitle());
 
        String author = entity.getAuthor();
        if (author != null) {
            stmt.bindString(4, author);
        }
 
        String coverUrl = entity.getCoverUrl();
        if (coverUrl != null) {
            stmt.bindString(5, coverUrl);
        }
 
        String desc = entity.getDesc();
        if (desc != null) {
            stmt.bindString(6, desc);
        }
 
        String wordCount = entity.getWordCount();
        if (wordCount != null) {
            stmt.bindString(7, wordCount);
        }
 
        String state = entity.getState();
        if (state != null) {
            stmt.bindString(8, state);
        }
 
        String category = entity.getCategory();
        if (category != null) {
            stmt.bindString(9, category);
        }
 
        String lastChapter = entity.getLastChapter();
        if (lastChapter != null) {
            stmt.bindString(10, lastChapter);
        }
 
        String lastUpdated = entity.getLastUpdated();
        if (lastUpdated != null) {
            stmt.bindString(11, lastUpdated);
        }
 
        String lastRead = entity.getLastRead();
        if (lastRead != null) {
            stmt.bindString(12, lastRead);
        }
 
        Integer chaptersCount = entity.getChaptersCount();
        if (chaptersCount != null) {
            stmt.bindLong(13, chaptersCount);
        }
 
        Integer currentChapter = entity.getCurrentChapter();
        if (currentChapter != null) {
            stmt.bindLong(14, currentChapter);
        }
 
        Integer currentPage = entity.getCurrentPage();
        if (currentPage != null) {
            stmt.bindLong(15, currentPage);
        }
 
        Boolean ifUpdated = entity.getIfUpdated();
        if (ifUpdated != null) {
            stmt.bindLong(16, ifUpdated ? 1L: 0L);
        }
 
        Boolean ifLocal = entity.getIfLocal();
        if (ifLocal != null) {
            stmt.bindLong(17, ifLocal ? 1L: 0L);
        }
 
        String bookSourceUrl = entity.getBookSourceUrl();
        if (bookSourceUrl != null) {
            stmt.bindString(18, bookSourceUrl);
        }
 
        String group = entity.getGroup();
        if (group != null) {
            stmt.bindString(19, group);
        }
 
        String diyTitle = entity.getDiyTitle();
        if (diyTitle != null) {
            stmt.bindString(20, diyTitle);
        }
 
        String diyAuthor = entity.getDiyAuthor();
        if (diyAuthor != null) {
            stmt.bindString(21, diyAuthor);
        }
 
        String diyDesc = entity.getDiyDesc();
        if (diyDesc != null) {
            stmt.bindString(22, diyDesc);
        }
 
        String diyCoverUrl = entity.getDiyCoverUrl();
        if (diyCoverUrl != null) {
            stmt.bindString(23, diyCoverUrl);
        }
 
        Boolean encrypted = entity.getEncrypted();
        if (encrypted != null) {
            stmt.bindLong(24, encrypted ? 1L: 0L);
        }
 
        HashMap varMap = entity.getVarMap();
        if (varMap != null) {
            stmt.bindString(25, varMapConverter.convertToDatabaseValue(varMap));
        }
 
        String catalogUrl = entity.getCatalogUrl();
        if (catalogUrl != null) {
            stmt.bindString(26, catalogUrl);
        }
 
        String catalogNextPageUrl = entity.getCatalogNextPageUrl();
        if (catalogNextPageUrl != null) {
            stmt.bindString(27, catalogNextPageUrl);
        }
    }

    @Override
    protected final void attachEntity(CollectionMediaBean entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public CollectionMediaBean readEntity(Cursor cursor, int offset) {
        CollectionMediaBean entity = new CollectionMediaBean( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // id
            cursor.getInt(offset + 1), // mediaType
            cursor.getString(offset + 2), // title
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // author
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // coverUrl
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // desc
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // wordCount
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // state
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // category
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // lastChapter
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // lastUpdated
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // lastRead
            cursor.isNull(offset + 12) ? null : cursor.getInt(offset + 12), // chaptersCount
            cursor.isNull(offset + 13) ? null : cursor.getInt(offset + 13), // currentChapter
            cursor.isNull(offset + 14) ? null : cursor.getInt(offset + 14), // currentPage
            cursor.isNull(offset + 15) ? null : cursor.getShort(offset + 15) != 0, // ifUpdated
            cursor.isNull(offset + 16) ? null : cursor.getShort(offset + 16) != 0, // ifLocal
            cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17), // bookSourceUrl
            cursor.isNull(offset + 18) ? null : cursor.getString(offset + 18), // group
            cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19), // diyTitle
            cursor.isNull(offset + 20) ? null : cursor.getString(offset + 20), // diyAuthor
            cursor.isNull(offset + 21) ? null : cursor.getString(offset + 21), // diyDesc
            cursor.isNull(offset + 22) ? null : cursor.getString(offset + 22), // diyCoverUrl
            cursor.isNull(offset + 23) ? null : cursor.getShort(offset + 23) != 0, // encrypted
            cursor.isNull(offset + 24) ? null : varMapConverter.convertToEntityProperty(cursor.getString(offset + 24)), // varMap
            cursor.isNull(offset + 25) ? null : cursor.getString(offset + 25), // catalogUrl
            cursor.isNull(offset + 26) ? null : cursor.getString(offset + 26) // catalogNextPageUrl
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, CollectionMediaBean entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setMediaType(cursor.getInt(offset + 1));
        entity.setTitle(cursor.getString(offset + 2));
        entity.setAuthor(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setCoverUrl(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setDesc(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setWordCount(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setState(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setCategory(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setLastChapter(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setLastUpdated(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setLastRead(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setChaptersCount(cursor.isNull(offset + 12) ? null : cursor.getInt(offset + 12));
        entity.setCurrentChapter(cursor.isNull(offset + 13) ? null : cursor.getInt(offset + 13));
        entity.setCurrentPage(cursor.isNull(offset + 14) ? null : cursor.getInt(offset + 14));
        entity.setIfUpdated(cursor.isNull(offset + 15) ? null : cursor.getShort(offset + 15) != 0);
        entity.setIfLocal(cursor.isNull(offset + 16) ? null : cursor.getShort(offset + 16) != 0);
        entity.setBookSourceUrl(cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17));
        entity.setGroup(cursor.isNull(offset + 18) ? null : cursor.getString(offset + 18));
        entity.setDiyTitle(cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19));
        entity.setDiyAuthor(cursor.isNull(offset + 20) ? null : cursor.getString(offset + 20));
        entity.setDiyDesc(cursor.isNull(offset + 21) ? null : cursor.getString(offset + 21));
        entity.setDiyCoverUrl(cursor.isNull(offset + 22) ? null : cursor.getString(offset + 22));
        entity.setEncrypted(cursor.isNull(offset + 23) ? null : cursor.getShort(offset + 23) != 0);
        entity.setVarMap(cursor.isNull(offset + 24) ? null : varMapConverter.convertToEntityProperty(cursor.getString(offset + 24)));
        entity.setCatalogUrl(cursor.isNull(offset + 25) ? null : cursor.getString(offset + 25));
        entity.setCatalogNextPageUrl(cursor.isNull(offset + 26) ? null : cursor.getString(offset + 26));
     }
    
    @Override
    protected final String updateKeyAfterInsert(CollectionMediaBean entity, long rowId) {
        return entity.getId();
    }
    
    @Override
    public String getKey(CollectionMediaBean entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(CollectionMediaBean entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getSiteSourceBeanDao().getAllColumns());
            builder.append(" FROM COLLECTION_MEDIA_BEAN T");
            builder.append(" LEFT JOIN SITE_SOURCE_BEAN T0 ON T.\"BOOK_SOURCE_URL\"=T0.\"SITE_HOST_URL\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected CollectionMediaBean loadCurrentDeep(Cursor cursor, boolean lock) {
        CollectionMediaBean entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        SiteSourceBean siteSourceBean = loadCurrentOther(daoSession.getSiteSourceBeanDao(), cursor, offset);
        entity.setSiteSourceBean(siteSourceBean);

        return entity;    
    }

    public CollectionMediaBean loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<CollectionMediaBean> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<CollectionMediaBean> list = new ArrayList<CollectionMediaBean>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<CollectionMediaBean> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<CollectionMediaBean> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
